from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from common.Utils import BlogDriver


# 博客首页测试用例

class BlogList:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = BlogDriver.driver
        self.driver.implicitly_wait(5)
        self.driver.get(self.url)

    # 测试首页（登录状态下）
    def ListTestByLogin(self):
        # 测试博客标题是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")
        # # 测试博客上传时间是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2) > svg")
        # # 测试按钮 +发布帖子 是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div")

        # 个人信息-检查昵称是否存在
        name = self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)").text
        print(name)

        # 添加屏幕截图
        BlogDriver.getScreeShot()

    # 测试首页（未登录状态下）
    def ListTestByNoLogin(self):
        # 测试博客标题是否存在
        try:
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")
        except NoSuchElementException as e:
            print("未找到该元素")

        # 添加屏幕截图
        BlogDriver.getScreeShot()

        BlogDriver.driver.quit()

# blogList = BlogList()
# blogList.ListTestByNoLogin()

























